/*
请判断一个链表是否为回文链表。
输入: 1->2->2->1
输出: true

进阶：
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题？

思路： 快慢指针找到中轴，中轴位置开始反转链表，两头开始判断
 */

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var isPalindrome = function (head) {
  let slow = head
  let fast = head
  while (fast.next && fast.next.next) {
    fast = fast.next.next
    slow = slow.next
  }
  let head2 = reverseList(slow)
  while (head2 && head) {
    if (head2.val !== head.val) {
      return false
    }
    head2 = head2.next
    head = head.next
  }
  return true
}

var reverseList = function (head) {
  let prev = null
  let cur = head
  while (cur !== null) {
    const r = cur.next
    cur.next = prev
    prev = cur
    cur = r
  }
  return prev
}
